/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "sle_advanced_setting_manager.h"
#include "sle_connect_event_handler.h"
#include "sle_connect_server.h"
#include "sle_connect_service.h"
#include "sle_connect_manager.h"
#include "sle_adapter_manager.h"
#include "sle_utils_remote_observer_list.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
namespace {
constexpr const char *SLE_CONNECT_RUNNER_WORK_THREAD = "SLE_CONNECT_RUNNER_WORK_THREAD";
}

class SleConnectServer::SleConnectServerImpl {
public:
    SleConnectServerImpl();
    ~SleConnectServerImpl();

    SleErrorCode CheckOperation();
    SleErrorCode RegisterConnectCallback(const sptr<ISleConnectCallback> &callback);
    SleErrorCode UnregisterConnectCallback(const sptr<ISleConnectCallback> &callback);
    SleErrorCode Connect(const SleAddress &address);
    SleErrorCode Disconnect(const SleAddress &address);
    SleErrorCode GetConnectId(const SleAddress &address, uint16_t &connectId);
    SleErrorCode GetConnectState(const SleAddress &address, int32_t &state);
    SleErrorCode GetConnectDeviceList(std::vector<SleConnectDevice> &devices, uint16_t &number);
    SleErrorCode UpdateConnectParam(uint16_t connectId, const SleConnectionParam &param);
    SleErrorCode UpdateAllConnectParam(std::vector<SleConnectDevice> &successUpdateDev);
    SleErrorCode GetRecomSendDataSize(uint16_t connectId, uint32_t &recomSendDataSize);
    SleErrorCode SendPairRequest(const SleAddress &address);
    SleErrorCode RemovePairedDevice(const SleAddress &address);
    SleErrorCode RemoveAllPairedDevices();
    SleErrorCode GetPairedDevicesNum(uint16_t &number);
    SleErrorCode GetPairedDevices(std::vector<SleAddress> &devices, uint16_t &number);
    SleErrorCode GetPairState(const SleAddress &address, uint8_t &state);
    SleErrorCode ReadRemoteRssi(uint16_t connectId);


    class SleConnectManagerCallbackImpl;
    std::unique_ptr<SleConnectManagerCallbackImpl> callbackImpl_ = nullptr;

    SleConnectService *connService_ = nullptr;
    SleConnectManager *connManager_ = nullptr;
    SleAdapterManager *adapterManager_ = nullptr;
    SleRemoteObserverList<ISleConnectCallback> callbacks_;
    std::shared_ptr<AppExecFwk::EventRunner> connectEventRunner_ = nullptr;
    std::shared_ptr<SleConnectEventHandler> connectEventHandler_  = nullptr;
};

class SleConnectServer::SleConnectServerImpl::SleConnectManagerCallbackImpl : public ISleConnectManagerCallback {
public:
    SleConnectManagerCallbackImpl(SleConnectServer::SleConnectServerImpl &impl) : impl_(impl){};
    ~SleConnectManagerCallbackImpl() = default;
    void OnConnectStateChange(uint16_t connectId, const SleAddress &address, SleConnectState state,
        SlePairState pairState, SleDisConnectReason reason) override
    {
        SLE_LOGD("OnConnectStateChange In");
        if (impl_.connectEventHandler_) {
            impl_.connectEventHandler_->PostAsyncTask(
                 [connectId, address, state, pairState, reason]() {
                     AdvancedSettingManager().GetInstance()->ASProcessConnectStateChange(connectId, address, state,
                         pairState, reason);

                     }, 0);
        }
        impl_.callbacks_.ForEach([connectId, &address, state, pairState, reason](sptr<ISleConnectCallback> callback) {
            callback->OnConnectStateChange(connectId, address, state, pairState, reason);
        });
    }

    void OnConnectParamUpdateComplete(
        uint16_t connectId, const SleConnectionParamUpdateEvt &param, SleErrorCode errCode) override
    {
        SLE_LOGD("OnConnectParamUpdateComplete In");
        if (impl_.connectEventHandler_) {
            impl_.connectEventHandler_->PostAsyncTask(
                [connectId, param, errCode]() {
                    AdvancedSettingManager().GetInstance()->ASProcessConnParamUpdateComplete(connectId, param, errCode);
                }, 0);
        }
        impl_.callbacks_.ForEach([connectId, &param, errCode](sptr<ISleConnectCallback> callback) {
            callback->OnConnectParamUpdateComplete(
                connectId, param.interval, param.latency, param.supervisionTimeout, errCode);
        });
    }

    void OnAuthComplete(
        uint16_t connectId, const SleAddress &address, const SleAuthInfo &info, SleErrorCode errCode) override
    {
        SLE_LOGD("OnAuthComplete In");
        SleAuthenticationInfo authInfo(info);
        impl_.callbacks_.ForEach([connectId, &address, &authInfo, errCode](sptr<ISleConnectCallback> callback) {
            callback->OnAuthComplete(connectId, address, authInfo, errCode);
        });
    }

    void OnPairComplete(uint16_t connectId, const SleAddress &address, SleErrorCode errCode) override
    {
        SLE_LOGD("OnPairComplete In");
        impl_.callbacks_.ForEach([connectId, &address, errCode](sptr<ISleConnectCallback> callback) {
            callback->OnPairComplete(connectId, address, errCode);
        });
    }

    void OnReadRssiComplete(uint16_t connectId, int8_t rssi, SleErrorCode errCode) override
    {
        SLE_LOGD("OnReadRssiComplete In");
        impl_.callbacks_.ForEach([connectId, rssi, errCode](sptr<ISleConnectCallback> callback) {
            callback->OnReadRssiComplete(connectId, rssi, errCode);
        });
    }

private:
    SleConnectServer::SleConnectServerImpl &impl_;
};

SleConnectServer::SleConnectServerImpl::SleConnectServerImpl()
{
    SLE_LOGI("SleConnectServerImpl In");
    callbackImpl_ = std::make_unique<SleConnectManagerCallbackImpl>(*this);
    SLE_ASSERT_AND_RETURN_LOG(callbackImpl_, "callbackImpl null error!");

    connService_ = SleConnectService::GetInstance();
    SLE_ASSERT_AND_RETURN_LOG(connService_, "connect service instance null error!");

    connManager_ = SleConnectManager::GetInstance();
    SLE_ASSERT_AND_RETURN_LOG(connManager_, "connect manager instance null error!");

    SleErrorCode ret = connManager_->RegisterConnectCallback(*callbackImpl_.get());
    SLE_ASSERT_AND_RETURN_LOG(ret == SLE_NO_ERROR, "register connect callback failed(%{public}d)!", ret);

    adapterManager_ = SleAdapterManager::GetInstance();
    SLE_ASSERT_AND_RETURN_LOG(adapterManager_, "adapter manager instance null error!");

    connectEventRunner_ = AppExecFwk::EventRunner::Create(SLE_CONNECT_RUNNER_WORK_THREAD);
    SLE_ASSERT_AND_RETURN_LOG(connectEventRunner_ != nullptr, "create connectEventRunner_ fail");
    connectEventHandler_ = std::make_shared<SleConnectEventHandler>(connectEventRunner_);
    SLE_ASSERT_AND_RETURN_LOG(connectEventHandler_ != nullptr, "create connectEventHandler_ fail");
}

SleConnectServer::SleConnectServerImpl::~SleConnectServerImpl()
{
    SLE_LOGI("~SleConnectServerImpl In");
    SLE_ASSERT_AND_RETURN_LOG(callbackImpl_, "callbackImpl null error!");
    SLE_ASSERT_AND_RETURN_LOG(connManager_, "connect manager instance null error!");
    SleErrorCode ret = connManager_->UnregisterConnnectCallback(*callbackImpl_.get());
    SLE_ASSERT_AND_RETURN_LOG(ret == SLE_NO_ERROR, "unregister connect callback failed(%{public}d)!", ret);
}

SleErrorCode SleConnectServer::SleConnectServerImpl::CheckOperation()
{
    SLE_ASSERT_AND_RETURN_RET_LOG(connService_, SLE_ERR_NOT_READY, "connect service instance null error!");
    SLE_ASSERT_AND_RETURN_RET_LOG(connManager_, SLE_ERR_NOT_READY, "connect manager instance null error!");
    SLE_ASSERT_AND_RETURN_RET_LOG(adapterManager_, SLE_ERR_NOT_READY, "adapter manager instance null error!");
    int32_t state;
    SleErrorCode ret = adapterManager_->GetState(state);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get sle state failed(%{public}d)!", ret);
    SLE_ASSERT_AND_RETURN_RET_LOG(state == SLE_HOST_STATE_ENABLED, SLE_ERR_STATUS_WRONG,
        "host state(%s) error!", SleDeviceStateStr(state));
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::RegisterConnectCallback(const sptr<ISleConnectCallback> &callback)
{
    SLE_LOGD("RegisterConnectCallback In");
    auto func = std::bind(&SleConnectServer::SleConnectServerImpl::UnregisterConnectCallback, this, std::placeholders::_1);
    SLE_ASSERT_AND_RETURN_RET_LOG(callbacks_.Register(callback, func), SLE_ERR_FAILED, "register connect callback failed!");
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::UnregisterConnectCallback(const sptr<ISleConnectCallback> &callback)
{
    SLE_LOGD("UnregisterConnectCallback In");
    SLE_ASSERT_AND_RETURN_RET_LOG(callbacks_.Unregister(callback), SLE_ERR_FAILED, "unregister connect callback failed!");
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::Connect(const SleAddress &address)
{
    SLE_LOGD("Connect In");
    SleErrorCode ret = connManager_->Connect(address);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "connect device failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::Disconnect(const SleAddress &address)
{
    SLE_LOGD("Disconnect In");
    SleErrorCode ret = connManager_->Disconnect(address);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "disconnect device failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::GetConnectId(const SleAddress &address, uint16_t &connectId)
{
    SLE_LOGD("GetConnectId In");
    SleErrorCode ret = connManager_->GetConnectId(address, connectId);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get connect id failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::GetConnectState(const SleAddress &address, int32_t &state)
{
    SLE_LOGD("GetConnectState In");
    SleErrorCode ret = connManager_->GetConnectState(address, state);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get connect state failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::GetConnectDeviceList(
        std::vector<SleConnectDevice> &devices, uint16_t &number)
{
    SLE_LOGD("GetConnectDeviceList In");
    SleErrorCode ret = connManager_->GetConnectDeviceList(devices, number);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get connected device list failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::UpdateConnectParam(uint16_t connectId, const SleConnectionParam &param)
{
    SLE_LOGD("UpdateConnectParam In");
    SleErrorCode ret = connManager_->UpdateConnectParam(connectId, param);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "update connect param failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::UpdateAllConnectParam(std::vector<SleConnectDevice> &successUpdateDev)
{
    SLE_LOGD("UpdateAllConnectParam In");
    SleErrorCode ret = AdvancedSettingManager::GetInstance()->UpdateAllConnectParam(successUpdateDev);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "update all connect param failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::GetRecomSendDataSize(uint16_t connectId, uint32_t &recomSendDataSize)
{
    SLE_LOGD("GetRecomSendDataSize In");
    SleErrorCode ret = AdvancedSettingManager::GetInstance()->GetRecomSendDataSize(connectId, recomSendDataSize);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get recommend send data size failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::SendPairRequest(const SleAddress &address)
{
    SLE_LOGD("SendPairRequest In");
    SleErrorCode ret = connService_->SendPairRequest(address);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "send pair request failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::RemovePairedDevice(const SleAddress &address)
{
    SLE_LOGD("RemovePairedDevice In");
    SleErrorCode ret = connService_->RemovePairedDevice(address);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "remove paired device failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::RemoveAllPairedDevices()
{
    SLE_LOGD("RemoveAllPairedDevices In");
    SleErrorCode ret = connService_->RemoveAllPairedDevices();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "remove all paired devices failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::GetPairedDevicesNum(uint16_t &number)
{
    SLE_LOGD("GetPairedDevicesNum In");
    SleErrorCode ret = connService_->GetPairedDevicesNum(number);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get paired devices number failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::GetPairedDevices(std::vector<SleAddress> &devices, uint16_t &number)
{
    SLE_LOGD("GetPairedDevices In");
    SleErrorCode ret = connService_->GetPairedDevices(devices, number);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get paired devices failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::GetPairState(const SleAddress &address, uint8_t &state)
{
    SLE_LOGD("GetPairState In");
    SleErrorCode ret = connService_->GetPairState(address, state);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get pair state failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::SleConnectServerImpl::ReadRemoteRssi(uint16_t connectId)
{
    SLE_LOGD("ReadRemoteRssi In");
    SleErrorCode ret = connService_->ReadRemoteRssi(connectId);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "read rssi failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleConnectServer::SleConnectServer()
{
    SLE_LOGI("SleConnectServer In");
    pimpl_ = std::make_unique<SleConnectServerImpl>();
    SLE_ASSERT_AND_RETURN_LOG(pimpl_, "pimpl null error!");
}

SleConnectServer::~SleConnectServer(){}

SleErrorCode SleConnectServer::RegisterConnectCallback(const sptr<ISleConnectCallback> &callback)
{
    SLE_LOGD("RegisterConnectCallback In");
    SLE_ASSERT_AND_RETURN_RET_LOG(callback, SLE_ERR_INVALID_PARAM, "callback null error!");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->RegisterConnectCallback(callback);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "register connect callback failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::UnregisterConnectCallback(const sptr<ISleConnectCallback> &callback)
{
    SLE_LOGD("UnregisterConnectCallback In");
    SLE_ASSERT_AND_RETURN_RET_LOG(callback, SLE_ERR_INVALID_PARAM, "callback null error!");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->UnregisterConnectCallback(callback);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "unregitster connect callback failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::Connect(const SleAddress &address)
{
    SLE_LOGD("Connect In");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->Connect(address);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "connect device failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::Disconnect(const SleAddress &address)
{
    SLE_LOGD("Disconnect In");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->Disconnect(address);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "disconnect device failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::GetConnectId(const SleAddress &address, uint16_t &connectId)
{
    SLE_LOGD("GetConnectId In");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->GetConnectId(address, connectId);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get connect id failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::GetConnectState(const SleAddress &address, int32_t &state)
{
    SLE_LOGD("GetConnectState In");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->GetConnectState(address, state);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get connect state failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::GetConnectDeviceList(std::vector<SleConnectDevice> &devices, uint16_t &number)
{
    SLE_LOGD("GetConnectDeviceList In");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->GetConnectDeviceList(devices, number);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get connected device list failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::UpdateConnectParam(uint16_t connectId, const SleConnectionParam &param)
{
    SLE_LOGD("UpdateConnectParam In, and set phy");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->UpdateConnectParam(connectId, param);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "update connect param failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::UpdateAllConnectParam(std::vector<SleConnectDevice> &successUpdateDev, uint16_t &number)
{
    SLE_LOGD("UpdateAllConnectParam In, and set phy");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->UpdateAllConnectParam(successUpdateDev);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "update connect param failed(%{public}d)!", ret);
    number = successUpdateDev.size();
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::GetRecomSendDataSize(uint16_t connectId, uint32_t &recomSendDataSize)
{
    SLE_LOGD("GetSendDataSize In");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->GetRecomSendDataSize(connectId, recomSendDataSize);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get recommend send data size failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}


SleErrorCode SleConnectServer::SendPairRequest(const SleAddress &address)
{
    SLE_LOGD("SendPairRequest In");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->SendPairRequest(address);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "send pair request failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::RemovePairedDevice(const SleAddress &address)
{
    SLE_LOGD("RemovePairedDevice In");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->RemovePairedDevice(address);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "remove paired device failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::RemoveAllPairedDevices()
{
    SLE_LOGD("RemoveAllPairedDevices In");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->RemoveAllPairedDevices();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "remove all paired devices failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::GetPairedDevicesNum(uint16_t &number)
{
    SLE_LOGD("GetPairedDevicesNum In");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->GetPairedDevicesNum(number);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get paired devices number failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::GetPairedDevices(std::vector<SleAddress> &devices, uint16_t &number)
{
    SLE_LOGD("GetPairedDevices In");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->GetPairedDevices(devices, number);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get paired devices failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::GetPairState(const SleAddress &address, uint8_t &state)
{
    SLE_LOGD("GetPairState In");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->GetPairState(address, state);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "get pair state failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}

SleErrorCode SleConnectServer::ReadRemoteRssi(uint16_t connectId)
{
    SLE_LOGD("ReadRemoteRssi In");
    SLE_ASSERT_AND_RETURN_RET_LOG(pimpl_, SLE_ERR_NOT_READY, "pimpl null error!");
    SleErrorCode ret = pimpl_->CheckOperation();
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, SLE_ERR_CHECKOUT_FAILED, "check operation failed(%{public}d)!", ret);
    ret = pimpl_->ReadRemoteRssi(connectId);
    SLE_ASSERT_AND_RETURN_RET_LOG(ret == SLE_NO_ERROR, ret, "read rssi failed(%{public}d)!", ret);
    return SLE_NO_ERROR;
}
} // SleStandard
} // NearLink
} // OHOS